package com.i2863.shiroDemo.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.i2863.shiroDemo.dto.ResultInfo;
import com.i2863.shiroDemo.entity.Log;
import com.i2863.shiroDemo.entity.LoginLog;
import com.i2863.shiroDemo.service.ILogService;
import com.i2863.shiroDemo.service.ILoginLogService;
import com.i2863.shiroDemo.util.FormatUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class SystemController extends BaseController {

    @Resource
    private ILogService ilogService;
    @Resource
    private ILoginLogService iloginLogService;

    @RequestMapping("loginLog/list")
    public String toLoginLogHtml() {
        return "system/login_log_list";
    }

    @RequestMapping("log/list")
    public String toLogHtml() {
        return "system/log_list";
    }

    @RequestMapping("site/list")
    public String toHtml() {
        return "system/site_list";
    }


    @ResponseBody
    @RequestMapping("log/listData")
    @RequiresPermissions("log:view")
    public ResultInfo<List<Log>> listLog(String userName, String operTime, Integer page, Integer limit) {
        Log log = new Log();
        log.setUserName(userName);
        QueryWrapper<Log> wrapper = new QueryWrapper<>(log);
        if (!StringUtils.isEmpty(operTime)) {
            wrapper.ge("create_time", FormatUtil.parseDate(operTime.split(" - ")[0] + " 00:00:00", null));
            wrapper.le("create_time", FormatUtil.parseDate(operTime.split(" - ")[1] + " 23:59:59", null));
        }
        wrapper.orderBy(true, false, "create_time");
        IPage<Log> pageObj = ilogService.page(new Page<>(page, limit), wrapper);
        return new ResultInfo<>(pageObj.getRecords(), pageObj.getTotal());
    }

    @RequestMapping("loginLog/listData")
    @RequiresPermissions("loginLog:view")
    public @ResponseBody
    ResultInfo<List<LoginLog>> listLoginLog(String userName, String loginTime, Integer page, Integer limit) {
        LoginLog loginLog = new LoginLog();
        loginLog.setUserName(userName);
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>(loginLog);
        if (!StringUtils.isEmpty(loginTime)) {
            wrapper.ge("create_time", FormatUtil.parseDate(loginTime.split(" - ")[0] + " 00:00:00", null));
            wrapper.le("create_time", FormatUtil.parseDate(loginTime.split(" - ")[1] + " 23:59:59", null));
        }
        wrapper.orderBy(true, false, "create_time");
        IPage<LoginLog> pageObj = iloginLogService.page(new Page<>(page, limit), wrapper);
        return new ResultInfo<>(pageObj.getRecords(), pageObj.getTotal());
    }
}
